package cn.tansanqinger.basics;
import sun.misc.Unsafe;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

interface IMessage{}
interface IChannel{}
abstract class AbstraceChannalMessage implements IMessage,IChannel{
    public void AbstractChannelMessage() {}
    public void AbstractChannelMessage(String username, String password) {}
}
//该类为定义方法，但是可以获取继承来的方法
class Person{}
class CloudMessage extends AbstraceChannalMessage implements IMessage,IChannel{
    public CloudMessage() {}
    public CloudMessage(String url, String userName, String password) {}
}
public class Test3 {
    public static void main(String[] args) {
        Class<?> clazzA = CloudMessage.class;
        System.out.println("【获取包的名称】"+clazzA.getPackage().getName());
        System.out.println("【获取继承父类】"+clazzA.getSuperclass().getName());
        System.out.println("【获取实现接口】"+ Arrays.toString(clazzA.getInterfaces()));
        //---------------获取构造方法----------------
        Class<?> clazzB = CloudMessage.class;
        Constructor<?> constructor[] = clazzB.getDeclaredConstructors();
        for(Constructor<?> cont : constructor) {
            System.out.println(cont);
        }
        //-------------获取方法--------------
        Class<?> clazz = Person.class;
        Method methods[] = clazz.getMethods();
        for(int i = 0 ; i < methods.length ; i ++) {
            System.out.print(Modifier.toString(methods[i].getModifiers())+" ");
            System.out.print(methods[i].getReturnType().getName()+" ");
            System.out.print(methods[i].getName()+"(");
            Class<?> params[] = methods[i].getParameterTypes();//获取所有参数
            for(int x = 0 ; x < params.length ; x++) {
                System.out.print(params[x].getSimpleName()+" arg-"+x);
                if(x<params.length -1) {
                    System.out.print(",");
                }
            }
            System.out.print(") ");
            Class<?> exps[] = methods[i].getExceptionTypes();
            if(exps.length > 0) {
                System.out.print("throws ");
                for(int x = 0 ; x < exps.length ; x++) {
                    System.out.print(exps[x].getSimpleName());
                    if(x <params.length -1) {
                        System.out.print(",");
                    }
                }
            }
        System.out.println();
        }
    }
}
